const userService = require("../server/user");
const jwt = require("jsonwebtoken");
const { JWT_SECRET } = require("../config");
const { wirteJSON } = require("../utils/func");
const path = require("path");
module.exports = class userController {
  static async login(ctx) {
    try {
      const { username, password, type } = ctx.request.body;
      const token = jwt.sign(
        {
          name: username,
        },
        JWT_SECRET,
        {
          expiresIn: 60 * 60,
        }
      );
      let data;
      if (type === "admin") {
        [data] = await userService.login(username, password, type);
      } else if (type === "user") {
        [data] = await userService.login(username, password);
      }
      console.log(data);
      if (data) {
        // 删除密码为了安全
        delete data["password"];

        wirteJSON(ctx, 200, "登入成功！", {
          ...data,
          token,
          message: "登入成功！！",
        });
      } else {
        wirteJSON(ctx, 401, "身份验证失败！！", null);
      }
    } catch (err) {
      console.log(err);
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  static async register(ctx) {
    try {
      const { username, password } = ctx.request.body;
      const data = await userService.register(username, password);
      if (data.affectedRows) {
        wirteJSON(ctx, 200, "数据插入成功！！", null);
      } else {
        wirteJSON(ctx, 404, "数据插入失败，请试试其它账号密码！！", null);
      }
    } catch (err) {
      console.log(err);
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  //修改密码
  static async updatePassword(ctx) {
    try {
      const { uid, password, repassword } = ctx.request.body;
      const data = await userService.updatePassword(uid, password, repassword);
      if (data.affectedRows) {
        wirteJSON(ctx, 200, "数据修改成功！！", "密码更新成功！！");
      } else {
        wirteJSON(ctx, 404, "数据修改失败，uid/密码错误！！", null);
      }
    } catch (err) {
      console.log(err);
      wirteJSON(ctx, 500, err.message, null);
    }
  }
  //修改头像
  static async updateAvatar(ctx) {
    try {
      const { uid } = ctx.request.body;
      const { file } = ctx.request.files;
      const fileName = path.basename(file.filepath);
      const avatar = "/uploads/" + fileName;
      const data = await userService.updateAvatar(uid, avatar);
      if (data.affectedRows) {
        wirteJSON(ctx, 200, "头像修改成功！！", avatar);
      } else {
        wirteJSON(ctx, 404, "头像修改失败，uid/头像错误！！", null);
      }
    } catch (err) {
      console.log(err);
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  //
  static async getUserData(ctx) {
    try {
      const { page } = ctx.request.body;
      const tableData = await userService.getUserData(page);
      const total = await userService.getSum();
      wirteJSON(ctx, 200, "获取数据成功！！", {
        tableData,
        total,
      });
    } catch (err) {
      console.log(err);
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  static async batchAddUser(ctx) {
    try {
      const { users } = ctx.request.body;
      const data = JSON.parse(users);
      const res = await userService.batchAddUser(data);
      if (res.affectedRows) {
        wirteJSON(ctx, 200, "数据插入成功！！", "数据插入成功！！");
      } else {
        wirteJSON(
          ctx,
          404,
          "数据插入失败，请试试其它账号密码！！",
          "数据插入失败，请试试其它账号密码！"
        );
      }
    } catch (err) {
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  static async batchUpdateUser(ctx) {
    try {
      const { users } = ctx.request.body;
      const data = JSON.parse(users);
      const res = await userService.batchUpdateUser(data);
      if (res) {
        wirteJSON(ctx, 200, "数据更新成功！！", "数据更新成功！！");
      } else {
        wirteJSON(
          ctx,
          404,
          "数据更新失败，请试试其它账号密码！！",
          "数据更新失败，请试试其它账号密码！"
        );
      }
    } catch (err) {
      wirteJSON(ctx, 500, err.message, null);
    }
  }

  static async batchDeteleUser(ctx) {
    try {
      const { users } = ctx.request.body;
      const data = JSON.parse(users);
      const res = await userService.batchDeteleUser(data);
      if (res) {
        wirteJSON(ctx, 200, "数据删除成功！！", "数据删除成功！！");
      } else {
        wirteJSON(ctx, 404, "数据删除失败", "数据删除失败");
      }
    } catch (err) {
      wirteJSON(ctx, 500, err.message, null);
    }
  }
};
